package com.guigu.www.erp.yinjiaxiong.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.www.erp.common.dao.SGatherMapper;
import com.guigu.www.erp.common.pojo.PageVo;
import com.guigu.www.erp.common.pojo.SGather;
import com.guigu.www.erp.yinjiaxiong.service.YJXSGatherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class YJXSGatherServiceImpl implements YJXSGatherService {
    @Autowired
    SGatherMapper sGatherMapper;
/*
获取审核标志为0的数据
 */
    @Override
    public PageVo<SGather> getSGather0(SGather sGather,Integer pageNum,Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<SGather> list = sGatherMapper.getcheckTag0(sGather);
        PageInfo<SGather> pageInfo = new PageInfo<>(list);
        PageVo<SGather> pageVo = new PageVo<>();
        pageVo.setTotal(pageInfo.getTotal());
        pageVo.setRows(pageInfo.getList());
        //System.out.println("pageVo----------"+pageVo);
        return pageVo;
    }

    @Override
    public PageVo<SGather> getAllsGather(SGather sGather, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<SGather> list = sGatherMapper.getAllsGather(sGather);
        PageInfo<SGather> pageInfo = new PageInfo<>(list);
        PageVo<SGather> pageVo = new PageVo<>();
        pageVo.setTotal(pageInfo.getTotal());
        pageVo.setRows(pageInfo.getList());
        //System.out.println("pageVo----------"+pageVo);
        return pageVo;
    }

    @Override
    public int insertSGather(SGather sGather) {
        //生成入库编号 入库单编号	401+当前日期+4位流水号
        String time="401";
        //用于登记时间
        SimpleDateFormat fom=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String format2 = fom.format(date);


        //用于组成入库编号
        SimpleDateFormat format=new SimpleDateFormat("yyyyMMdd");
        String format1 = format.format(date);
        time+=format1;
        System.out.println("format1==="+format1);
        //查询上一条最大编号
        String s =sGatherMapper.getMaxgatherId();
        //System.out.println("s======="+s);
    //查询最大编号的方法
        if (s==null){
            time+="0001";
        }else {
            Integer i = Integer.parseInt(s.substring(11));
            i++;
            String str=i+"";
            if (str.length()==1){
                str="000"+str;
            }else if (str.length()==2){
                str="00"+str;
            }else if (str.length()==3){
                str="0"+str;
            }
            time+=str;
        }

        //System.out.println("time========="+time);
        sGather.setGatherId(time);
        sGather.setRegisterTime(format2);
        sGather.setCheckTag("0");//审核标记为0
        sGather.setStoreTag("1");//库存标记已登记
        //System.out.println("sGather======"+sGather);
        return sGatherMapper.insertSelective(sGather);
    }

    @Override
    public int updatesGathercheckTag(SGather sGather) {
        return sGatherMapper.updateByPrimaryKeySelective(sGather);
    }

    @Override
    public PageVo<SGather> getSGather1(SGather sGather, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<SGather> list = sGatherMapper.getcheckTag1(sGather);
        PageInfo<SGather> pageInfo = new PageInfo<>(list);
        PageVo<SGather> pageVo = new PageVo<>();
        pageVo.setTotal(pageInfo.getTotal());
        pageVo.setRows(pageInfo.getList());
        //System.out.println("pageVo----------"+pageVo);
        return pageVo;
    }

    @Override
    public int updateSGatherattemper(Integer id) {
        SGather sGather = new SGather();
        Date date = new Date();
        //获取当前时间
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = simpleDateFormat.format(date);
        sGather.setId(id);
        sGather.setAttemper("尹家雄");
        sGather.setAttemperTime(format);
        //System.out.println("修改入库表的时间=========="+sGather);
        //System.out.println("");
        return sGatherMapper.updateByPrimaryKeySelective(sGather);
    }

    @Override
    public int ByIdupdatesGatherstoreTag(Integer id) {
        return sGatherMapper.ByIdupdatesGatherstoreTag(id);
    }

    @Override
    public PageVo<SGather> getstoreTag2(SGather sGather, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<SGather> list = sGatherMapper.getstoreTag2(sGather);
        PageInfo<SGather> pageInfo = new PageInfo<>(list);
        PageVo<SGather> pageVo = new PageVo<>();
        pageVo.setTotal(pageInfo.getTotal());
        pageVo.setRows(pageInfo.getList());
        //System.out.println("pageVo----------"+pageVo);
        return pageVo;
    }

    @Override
    public int ByGatherIdupdatesgatheredAmount(Integer parentId, Integer sumAmount) {
        return sGatherMapper.ByGatherIdupdatesgatheredAmount(parentId,sumAmount);
    }

    @Override
    public PageVo<SGather> getstoreTag3(SGather sGather, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<SGather> list = sGatherMapper.getstoreTag3(sGather);
        PageInfo<SGather> pageInfo = new PageInfo<>(list);
        PageVo<SGather> pageVo = new PageVo<>();
        pageVo.setTotal(pageInfo.getTotal());
        pageVo.setRows(pageInfo.getList());
        //System.out.println("pageVo----------"+pageVo);
        return pageVo;
    }

    @Override
    public void updatecheckerAndcheckTime(SGather sGather) {
        sGather.setChecker("尹家雄");
        Date date = new Date();
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = simpleDateFormat.format(date);
        sGather.setCheckTime(format);
        //System.out.println("复核数据==="+sGather);
        sGatherMapper.updateByPrimaryKeySelective4(sGather);
    }

    @Override
    public PageVo<SGather> getGatherstoreTagGreater2(SGather sGather, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<SGather> list = sGatherMapper.getGatherstoreTagGreater2(sGather);
        PageInfo<SGather> pageInfo = new PageInfo<>(list);
        PageVo<SGather> pageVo = new PageVo<>();
        pageVo.setTotal(pageInfo.getTotal());
        pageVo.setRows(pageInfo.getList());
        //System.out.println("pageVo----------"+pageVo);
        return pageVo;
    }
}
